package day36;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author tongchen
 * @create 2023-04-26 21:25
 */
public class Main {
    public static void main(String[] args) {
        //定义输入
        Scanner scanner=new Scanner(System.in);
        while (scanner.hasNext()){
            int m=scanner.nextInt();
            if(m==0){
                break;
            }
            String pre=scanner.next();
            String post=scanner.next();
            //计算总数
            System.out.println(calSum(m, pre, post));
        }
    }

    private static long calSum(int m, String pre, String post) {
        //如果只有一个数值
        if(pre==null||pre.length()<=1){
            return 1;
        }
        //将所有分组存入队列中
        List<SubPrePost>list=splitString(pre,post);
        long treeCount=calCommon(m,list.size());
        //遍历
        for (SubPrePost postOne:list) {
            treeCount*=calSum(m,postOne.preOrder,postOne.postOrder);
        }
        return treeCount;
    }

    private static List<SubPrePost> splitString(String pre, String post) {
        List<SubPrePost>list=new ArrayList<>();
        //获取首位置
        int postRoot=0;
        int preRoot=1;
        while(preRoot<pre.length()){
            char ch=pre.charAt(preRoot);
            int postIndex=post.indexOf(ch);
            int count=postIndex-postRoot+1;
            //遍历将数组放入集合
            list.add(new SubPrePost(pre.substring(preRoot, preRoot+count),post.substring(postRoot, postRoot+count)));
            //下标向下
            preRoot+=count;
            postRoot+=count;
        }
        return list;
    }
    //求fac
    public static long fac(int n){
        int res=1;
        for(int i=1;i<=n;++i){
            res*=i;
        }
        return res;
    }
    //求每次的个数
    public static long calCommon(int m,int n){
        m = Math.min(m, n - m);
        int res=1;
        for (int i = n-m+1; i <=n ; i++) {
            res*=i;
        }
        return res/fac(m);
    }
}
class SubPrePost{
    public SubPrePost(String pre, String post){
        preOrder = pre;
        postOrder = post;
    }
    String preOrder;
    String postOrder;
}

